//
//  AddOrDeleteView.m
//  Edumap
//
//  Created by 刘建峰 on 16/8/24.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "AddOrDeleteView.h"

@interface AddOrDeleteView ()
@property(nonatomic,strong)UILabel * numLabel;
@property(nonatomic,strong)void (^complect)(NSInteger index);

@end

@implementation AddOrDeleteView

- (instancetype)initWithFrame:(CGRect)frame complect:(void (^)(NSInteger))complect{

    if (self = [super initWithFrame:frame]) {
        _complect = complect;
        
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius =4*VerticalRatio();
        self.layer.borderWidth = 1;
        self.layer.borderColor = [UIColor colorWithWhite:0.863 alpha:1.000].CGColor;
        
        CGFloat width = CGRectGetWidth(frame)/VerticalRatio();
        CGFloat height = CGRectGetHeight(frame)/VerticalRatio();
        
        _numLabel = [[UILabel alloc]initWithFrame:flexibleFrame(CGRectMake(height, 0, width-2*height, height), NO)];
        _numLabel.font = [UIFont systemFontOfSize:13*VerticalTexeRatio()];
        _numLabel.textColor = [UIColor colorWithRed:0.141 green:0.400 blue:0.659 alpha:1.000];
        _numLabel.textAlignment = NSTextAlignmentCenter;
        _numLabel.text = @"1";
        [self addSubview:_numLabel];
        
        
        for (int i = 0; i<2; i++) {
            UIButton * Button = [UIButton buttonWithType:UIButtonTypeCustom];
            Button.frame = flexibleFrame(CGRectMake((width-height)*i, 0, height, height), NO);
            Button.tag = 100+i;
            [Button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"addOrDeleteNum%d",i+1]] forState:UIControlStateNormal];
            [Button addTarget:self action:@selector(addOrDelete:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:Button];
            
            
            UIView * line = [[UIView alloc]initWithFrame:flexibleFrame(CGRectMake(height + (width-2*height)*i, 7, 1, height-14), NO)];
            line.backgroundColor = [UIColor colorWithWhite:0.863 alpha:1.000];
            [self addSubview:line];
        }

    }
    
    return self;
}

- (void)addOrDelete:(UIButton *)sender{
    NSInteger num = [_numLabel.text integerValue];
    if (sender.tag == 100) {
        if (num > 1) {
            num -= 1;
        }
    }else{
            num += 1;
    }
     _numLabel.text = [NSString stringWithFormat:@"%ld",num];
    _complect(num);
}

@end
